class LinesController < ApplicationController
  before_filter :login_required
  before_filter :get_story
  before_filter :check_user,      :except => [:create, :new]
  before_filter :check_same_user, :only => [:create]
  before_filter :check_last_line, :except => [:create, :new]
  before_filter :check_restrictions, :only => [:create, :update]

  def new
    @line =Line.new
    respond_to do |format|
      format.html
      format.xml { render :xml => @line }
    end
  end

  # GET /lines/1/edit
  def edit
  end

  # POST /lines
  # POST /lines.xml
  def create
    @line = Line.new(params[:line])
    @line.story = @story
    check_line_length(@line.line, @story)
    @line.user = current_user
    
    respond_to do |format|
      if !flash[:notice] && @line.save
        flash[:notice] = 'Line was successfully created.'
        format.html { redirect_to(@story) }
        format.xml  { render :xml => @line, :status => :created, :location => @line }
      else
        flash[:notice] ||= 'Line was not successfully created.'
        format.html { redirect_to(@story) }
        format.xml  { render :xml => @line.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /lines/1
  # PUT /lines/1.xml
  def update
    check_line_length(@line.line, @story)
    respond_to do |format|
      if !flash[:notice] && @line.update_attributes(params[:line])
        flash[:notice] = 'Line was successfully updated.'
        format.html { redirect_to(story_path(@story)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @line.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /lines/1
  # DELETE /lines/1.xml
  def destroy
    @line.destroy

    respond_to do |format|
      format.html { redirect_to(story_path(@story)) }
      format.xml  { head :ok }
    end
  end
  
  protected
  
  def get_story
    @story = Story.find(params[:story_id])
  end
  
  def check_user
    @line = Line.find(params[:id])
    if @line.user_id != current_user.id && !current_user.moderator
      access_denied
    end
  end
  
  def access_denied
    if logged_in? && !current_user.moderator
      flash[:notice] = 'Hands off! That is not your line.'
      redirect_back_or_default(stories_url)
    else
      super
    end
  end


  def check_same_user
    if( @story.lines.length != 0  && !current_user.moderator && @story.lines[-1].user == current_user )
      flash[:notice] = 'Let someone else get a turn!'
      redirect_back_or_default(stories_url)
    end
  end 
  
  def check_restrictions 
    if (@story.typ && ! @story.typ.eql?('Prose'))
      result = @story.check_restrictions(params[:line][:line])
      if( !result[0]  )
        flash[:notice] = result[1]
        redirect_back_or_default(stories_url) 
      end
    end
  end
  
  def check_last_line
    @line = Line.find(params[:id])
    if @line.id != @story.lines[-1].id && !current_user.moderator
      flash[:notice] = "You can only modify the last line of a story."
      redirect_back_or_default(story_url(@story))
    end
  end
  
  def check_line_length(line, story)
    if line.length > story.max_line_length
      flash[:notice] = "The line cannot exceed " + story.max_line_length.to_s + " characters!"
  #    redirect_back_or_default(story_url(story))
    end
  end

end
